{include file="public/header,public/nav,public/fixbar" title="我的推广-<?php echo request()->pc_info['seo_title']?>"/}
<div class="layui-body">
    <div class="layui-container">
        <div class="layui-col-space15">
            <!-- 菜单栏 -->
            <div class="layui-col-xs12 layui-col-sm4 layui-col-md4 layui-col-lg3">
                <div class="layui-main user-main min-height-90vh">
                    {include file="user/menu"/}
                </div>
            </div>
            <div class="layui-col-xs12 layui-col-sm8 layui-col-md8 layui-col-lg9">
                <div class="layui-main user-main min-height-90vh">
                    <h2>我的推广</h2>
                    <div class="layui-tab layui-tab-brief padding-bottom-10">
                        <ul class="layui-tab-title">
                            {foreach $getPcProductType as $key => $v}
                            <li class="{if $key == 1}layui-this{/if} select-type" data-type="{$key}">{$v}</li>
                            {/foreach}
                        </ul>
                    </div>
                    <div class="layui-row">
                        <div class="layui-col-space15 scrollbar height-700" id="promotion-list">
                            
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{include file="public/footer"/}
<script type="text/javascript">
    layui.use(['flow','layer'], function(){
        var layer = layui.layer, flow = layui.flow, $=layui.jquery;
        
        var product_type = 1;
        $(function(){
            loadList(product_type);  // 加载列表
        })

        // 根据类型读取
        $(".select-type").on('click', function(){
            product_type = $(this).data('type');
            $('#promotion-list').html('');
            loadList(product_type);
        });

        // 分页加载列表
        function loadList(product_type){
            var limit = 10;//每次请求的条数
            flow.load({
                elem: '#promotion-list' //指定列表容器
                ,scrollElem: '#promotion-list'//滚动条所在元素
                ,end: '—— 已经到底了~ ——'
                ,done: function(page, next){ //到达临界点（默认滚动触发），触发下一页的回调
                    $.ajax({
                        type: "post",
                        dataType: "json",
                        data: {'page': page, 'limit':limit, 'product_type':product_type},//请求的页码和每页显示条数
                        url: '/user/promotion',
                        success: function (res) {
                            var lis = [];
                            if(res.code == 0){
                                if (res.data.length > 0) {
                                    
                                    var newsHtml = '';
                                    layui.each(res.data, function(index, item){
                                        var img = `<img src="${item.head_imgs}" class="user-course-img">`;
                                        if (item.product_type == 1) {
                                            img = `<img src="${item.head_imgs}" class="user-goods-img">`;
                                        } else if (item.product_type == 10){
                                            img = `<div class="justify-content-center">
                                                        <img style="background-color: ${item.background_color};margin-right: 0px;" src="${item.head_imgs}" class="user-goods-img">
                                                        <span class="exam-sub-title">${item.sub_title}</span>
                                                    </div>`;
                                        }
                                        newsHtml += `<div class="layui-col-xs12 layui-col-sm6 layui-col-md6">
                                                        <div class="layui-card product-item">
                                                            <a href="${item.href}">
                                                                <div class="layui-card-body">
                                                                    <div class="item">
                                                                        ${img}
                                                                        <div class="detail height-80">
                                                                            <div class="product-title layui-ellips">
                                                                                <span class="item-title-label">${item.product_type_text}</span>${item.name}
                                                                            </div>
                                                                            <p style="color:#999;margin-top: 5px;">佣金：<span class="layui-font-red">￥<b class="layui-font-16">${item.min_commission} ~ ${item.max_commission}</b></span></p>
                                                                        </div>
                                                                    </div>
                                                                </div>
                                                            </a>
                                                            <div class="layui-card-footer">
                                                                <p class="text">佣金比例：<span class="layui-font-16">${item.ratio}%</span></p>
                                                                <a class="layui-btn layui-btn-primary layui-btn-sm share" data-id="${item.id}"><i class="layui-icon layui-icon-share"></i> 分享</a>
                                                            </div>
                                                        </div>
                                                    </div>`;
                                    });
                                    
                                    lis.push(newsHtml);
                                    //执行下一页渲染，第二参数为：满足“加载更多”的条件，即后面仍有分页
                                    //res.total为Ajax返回的总页数，只有当前页小于总页数的情况下，才会继续出现加载更多
                                    next(lis.join(''), page < Math.ceil(res.count/limit));
                                    
                                } else {
                                    $('#promotion-list').html('<div class="layui-empty"><img src="/static/images/nodata/no_promotion.png"><p>暂无推广商品~</p></div>');
                                }
                            } else {
                                layer.msg(res.msg, {icon:2, time: 1500});
                            }
                        }
                        ,error:function(e){
                            layer.closeAll();
                            layer.msg("请求失败："+e.msg, {icon:2, time: 1500});
                        },
                    });
                }
            });
        }

        // 分享
        $(document).on('click', '.share',function(){
            var id = $(this).data('id');
            var index = layer.open({
                skin: 'layui-layer-radius', // 弹窗主题样式
                offset: 'auto',
                title: '商品分享',
                type: 2,
                anim: 'slideUp',
                shade: 0.3,
                shadeClose: true,
                area: ['375px', '640px'],
                content: '/user/promotion_share?id='+id
            });
            var device = layui.device('mobile');
            if (device.mobile) {
                layer.full(index);
            }
        })
    });

</script>